using System;
using System.CodeDom.Compiler;
using Microsoft.JScript;

namespace Weever.Json.Binding.Tests
{
    class JsUtility
    {
        static JsUtility()
        {
            JScriptCodeProvider codeProfider = new JScriptCodeProvider();
            string code = @"
class CompiledJSUtility
{
    function Eval(script: String) // public method
    {
        return eval(script);
    }
} 
";
            CompilerResults result = codeProfider.CompileAssemblyFromSource(new CompilerParameters(), code);
            object obj = result.CompiledAssembly.CreateInstance("CompiledJSUtility");

            evalFunctor = (Functor)MulticastDelegate.CreateDelegate(typeof(Functor), obj, "Eval");
        }

        private delegate object Functor(string args);

        private static Functor evalFunctor;

        public static object Eval(string script)
        {
            return evalFunctor(script);
        }

        public static JSObject FromJSON(string json)
        {
            return (JSObject)Eval("(" + json + ")");
        }
    }
}
